/*
 * Copyright (c)  2015~2020, xforceplus
 * All rights reserved.
 * Project:datacare-orm
 * Id: Query.java   2020-10-07 20-31-21
 * Author: Evan
 */
package cn.datacare.orm.query;

import cn.datacare.orm.util.StringUtils;

import java.util.Collection;
import java.util.List;

import static cn.datacare.orm.util.StringUtils.EMPTY;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: 2015~2020
 * </p>
 * <p>
 * Company/Department: xforceplus
 * </p>
 *
 * @author Evan
 * <b>Creation Time:</b> 2020-10-07 20-31-21
 * @since V1.0
 */
public interface Query<T extends StringQuery> extends QueryCondition<T>,QueryExecute<T>,StringQuery<Query>{

    /***
     * 获取一个新的条件
     * @return  Query<T>
     */
    Query<T> condition();

    /***
     * 拼接SQL
     * @param sqlPart SqlPart
     * @return Query<T>
     */
    Query<T> appendSql(String sqlPart);

    /**
     *  选择类型
     * @param clazz
     * @param columns
     * @return List<K>
     */

    <K> List<K> selectByType(Class<K> clazz, String... columns);


    /**
     * 过滤空和NULL的值，
     * 如果为空或者null则不增加查询条件
     * @param value
     * @return StrongValue
     */
    default StrongValue filterEmpty(Object value) {
        return new StrongValue() {
            @Override
            public boolean isEffective() {
                if (value == null) {
                    return false;
                }
                //校验空值
                if (value instanceof String) {
                    return !EMPTY.equals(value);
                }

                if (value instanceof Collection) {
                    return !((Collection) value).isEmpty();
                }
                return true;
            }

            @Override
            public Object getValue() {
                return value;
            }
        };
    }

    /**
     * 过滤空和NULL的值，
     * 如果为空或者null则不增加查询条件
     * @param value
     * @return StrongValue
     */
    default  StrongValue filterNull(Object value) {
        return new StrongValue() {
            @Override
            public boolean isEffective() {
                return value != null;
            }

            @Override
            public Object getValue() {
                return value;
            }
        };
    }
}
